<?php 
namespace Home\Controller;
use Think\Controller;
session_start();
class UserController extends Controller{
  //用户注册
	public function register(){
     if(IS_POST){
     	$user=I('post.user');
     	$pwd=md5(I('post.pwd'));
     	$register=D('User');
     	$res=$register->register($user,$pwd);
     	 if($res==1){
            $this->assign('success','注册成功，请登录');
            $this->display('success');
     	 }else if($res==2){
        $this->assign('error','用户名已存在');
     	 	$this->display('error');
     	 }
     }else{
     	$this->assign('error','不是post表单');
        $this->display('error');
     }
	}
  //用户登录
	public function login(){
		if(IS_POST){
        $user=I('post.user');
     	$pwd=md5(I('post.pwd'));
     	$login=D('User');
     	$res=$login->login($user,$pwd);
     		if($res==1){
              $_SESSION['username']=$user;
              $this->assign('success','欢迎回来');
              $this->display('success');
     		}else{
          $this->assign('error','用户名和密码不匹配');
               $this->display('error');
     		}
     	}
		else{
			$this->assign('error','不是post表单');
        $this->display('error');
		}

	}

	public function index(){
		$this->display('user');
    //展示注册登录页面
	}
}

 ?>